﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer.DAL;
using DataAccessLayer;
using Common;
namespace BestBuy.UserViews
{
    public partial class ShippingDetailInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = UserDAL.GetUserIDByUsername(HttpContext.Current.User.Identity.Name);
                ShippingDetail shipping = ShippingDetailDAL.GetShippingDetailByIDUser(id);
                UserSetting user = UserSettingDAL.GetUserSettinglByIDUser(id);
                if (shipping == null )
                {
                    Response.Redirect("~");
                    return;
                }
                Country.Text = shipping.Country;
                City.Text = shipping.City;
                Phone.Text = shipping.PhoneNumber;
                Address.Text = shipping.Address;
                if (user == null)
                {
                    YesEmailBuy.Checked = true;
                    YesEmailSetQuestions.Checked = true;
                }
                else
                {
                    bool emailType = user.NotifyNewBid;
                    if (emailType == true)
                    {
                        YesEmailBuy.Checked = true;
                    }
                    else
                    {
                        NoEmailBuy.Checked = true;
                    }
                    bool questionType = user.NotifyNewQuestion;
                    if (questionType == true)
                    {
                        YesEmailSetQuestions.Checked = true;
                    }
                    else
                    {
                        NoEmailSetQuestions.Checked = true;
                    }
                }
               
            }
        }

        protected void ChangePassButton_Click(object sender, EventArgs e)
        {
            var emp = new ShippingDetail()
            {
                Country = Country.Text.Trim(),
                City = City.Text.Trim(),
                PhoneNumber = Phone.Text.Trim(),
                Address = Address.Text.Trim(),
            };
            ShippingDetailDAL.UpdateShippingDetail(emp);
            BuyerInfoMessage.Show("Successful update personal information.", UserControls.MessagePanel.MessageType.Success);
        }

        protected void UpdateShippingDetailInfoButton_Click1(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            int id = UserDAL.GetUserIDByUsername(HttpContext.Current.User.Identity.Name);
            ShippingDetail shipping = ShippingDetailDAL.GetShippingDetailByIDUser(id);
            if (shipping == null)
            {
                Response.Redirect("~");
                return;
            }
            shipping.Country = Country.Text.Trim();
            shipping.City = City.Text.Trim();
            shipping.PhoneNumber = Phone.Text.Trim();
            shipping.Address = Address.Text.Trim();

            ShippingDetailDAL.UpdateShippingDetail(shipping);

            BuyerInfoMessage.Show("Successful update personal information.", UserControls.MessagePanel.MessageType.Success);
        }

        
       
        protected void UpdateUserReferenceInfoButton_Click1(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }
            int userType = YesEmailBuy.Checked ? 1 : 0;
            int id = UserDAL.GetUserIDByUsername(HttpContext.Current.User.Identity.Name);
            UserSetting user = UserSettingDAL.GetUserSettinglByIDUser(id);
            if (user == null)
            {
                Response.Redirect("~");
                return;
            }
            int emailQuestion= YesEmailSetQuestions.Checked ? 1 : 0;
            if (emailQuestion == 1)
            {
                user.NotifyNewQuestion = true;
            }
            else
            {
                user.NotifyNewQuestion = false; 
            }
            int emailBuy = YesEmailBuy.Checked ? 1 : 0;
            if (emailBuy == 1)
            {
                user.NotifyNewBid = true;
            }
            else
            {
                user.NotifyNewBid = false;
            }
            UserSettingDAL.UpdateUserSetting(user);

            BuyerInfoMessage.Show("Successful update personal information.", UserControls.MessagePanel.MessageType.Success);
         }
    }
}